#!/bin/bash
set -e
set -x
MYSELF="${0##*/}"

URL="$1"
PACKAGE="$2"

if [ -z "$URL" ]; then
	echo "Usage: ${MYSELF} <tarball-URL>"
	exit 1
fi

filename=${URL##*/}
if [ -z "$PACKAGE" ]; then
	PACKAGE="${filename%-*.tar.*}"
fi
VERSION="${filename##*-}"
VERSION="${VERSION%.tar.*}"

wget -N ${URL}
mkdir -p -m755 "${PACKAGE}"
tar -x -a -f "$filename" --strip-components=1 -C "$PACKAGE"
git add "$PACKAGE"

cat > cmsg.$$.txt <<-EOF
Imported ${PACKAGE} version ${VERSION}

${URL}
EOF

sha256sum "$filename" >> cmsg.$$.txt

git commit -F "cmsg.$$.txt"
rm -f "cmsg.$$.txt"
